function MalchezaarSummonInfernal(pUnit, Event)
    x=pUnit:GetX();
    y=pUnit:GetY();
    x=pUnit:GetZ();
    o=pUnit:GetO();
    pUnit:SpawnCreature(17646, x, y, x, o, 50, 180000)
    if math.random() < 0.2 then
    pUnit:SendChatMessage(14, 0, "All realities, all dimensions are open to me!")
    pUnit:PlaySoundToSet(9224)
    else
    end
end


function MalchezaarEnfeeble(pUnit, Event)
    pUnit:CastSpellOnTarget(30843,pUnit:GetRandomPlayer(7))
    pUnit:CastSpellOnTarget(30843,pUnit:GetRandomPlayer(7))
    pUnit:CastSpellOnTarget(30843,pUnit:GetRandomPlayer(7))
    pUnit:CastSpellOnTarget(30843,pUnit:GetRandomPlayer(7))
    pUnit:CastSpellOnTarget(30843,pUnit:GetRandomPlayer(7))
end


function MalchezaarShadowNova(pUnit, Event)
    pUnit:FullCastSpell(30852)
end


function MalchezaarSWPain(pUnit, Event)
        pUnit:FullCastSpellOnTarget(30854,pUnit:GetMainTank())
        pUnit:FullCastSpellOnTarget(30854,pUnit:GetRandomPlayer(0))
end


function MalchezaarThrash(pUnit, Event)
        pUnit:CastSpell(3391)
end


function MalchezaarAmplify(pUnit, Event)
        pUnit:FullCastSpellOnTarget(39095,pUnit:GetRandomPlayer(0))
end


function MalchezaarSunder(pUnit, Event)
        pUnit:FullCastSpellOnTarget(30901,pUnit:GetMainTank())
end


function MalchezaarCleave(pUnit, Event)
    pUnit:CastSpell(39174)
end    

function MalchezaarPhases(pUnit, Event)
    if pUnit:GetHealthPct() > 60 and Didthat == 0 then
        pUnit:RemoveEvents()
        pUnit:RegisterEvent("MalchezaarPhases", 3000, 0)
        pUnit:SendChatMessage(12, 0, "Phase 1")
        pUnit:SendChatMessage(14, 0, "Madness has brought you here to me. I shall be your undoing!")
        pUnit:PlaySoundToSet(9218)
        pUnit:RegisterEvent("MalchezaarSummonInfernal",45000,0)
        pUnit:RegisterEvent("MalchezaarEnfeeble",40000,0)
        pUnit:RegisterEvent("MalchezaarShadowNova",15000,0)
        pUnit:RegisterEvent("MalchezaarSWPain",25000,0)
        Didthat = 1
    else
    end

    
    if  pUnit:GetHealthPct() < 60 and pUnit:GetHealthPct() > 30 and Didthat == 1 then
        pUnit:RemoveEvents()
        pUnit:RegisterEvent("MalchezaarPhases", 3000, 0)
        pUnit:SendChatMessage(12, 0, "Phase 2")
        pUnit:SendChatMessage(14, 0, "Simple fools! Time is the fire in which you'll burn!")
        pUnit:PlaySoundToSet(9220)
        pUnit:RegisterEvent("MalchezaarSummonInfernal",45000,0)
        pUnit:RegisterEvent("MalchezaarEnfeeble",30000,0)
        pUnit:RegisterEvent("MalchezaarShadowNova",25000,0)
        pUnit:RegisterEvent("MalchezaarThrash",15000,0)
        pUnit:RegisterEvent("MalchezaarSunder",10000,0)
        pUnit:RegisterEvent("MalchezaarCleave",8000,0)
        Didthat = 2
    else
    end
        
        
    if  pUnit:GetHealthPct() < 30 and Didthat == 2 then
        pUnit:RemoveEvents()
        pUnit:RegisterEvent("MalchezaarPhases", 3000, 0)        
        pUnit:SendChatMessage(12, 0, "Phase 3")
        pUnit:SendChatMessage(14, 0, "How can you withstand against such overhelming power?")
        pUnit:PlaySoundToSet(9321)
        pUnit:RegisterEvent("MalchezaarSummonInfernal",15000,0)
        pUnit:RegisterEvent("MalchezaarSWPain",20000,0)
        pUnit:RegisterEvent("MalchezaarShadowNova",10000,0)
        pUnit:RegisterEvent("MalchezaarAmplify",15000,0)
        Didthat = 3
    else
    end
end

function PrinceMalchezaar(pUnit, Event)
        Didthat = 0
        pUnit:RegisterEvent("MalchezaarPhases", 3000, 0)
end
RegisterUnitEvent(15690, 1, "PrinceMalchezaar")


function MalchezaarDead(pUnit, Event)
        pUnit:Emote(16)
        pUnit:SendChatMessage(14, 0, "I refuse to concede defeat. I am a prince of the Eredar! I am...")
        pUnit:PlaySoundToSet(9221)
end
RegisterUnitEvent(15690, 4, "MalchezaarDead")


function PrinceMalchezaar_YellOnKill(pUnit, Event)
        pUnit:SendChatMessage(14, 0, "Your greed, your foolishness has brought you to this end.")
        pUnit:PlaySoundToSet(9318)
end
RegisterUnitEvent(15690, 3, "PrinceMalchezaar_YellOnKill")


function MalchezaarOnLeaveCombat(pUnit)
    pUnit:RemoveEvents()
end
RegisterUnitEvent(15690, 2, "MalchezaarOnLeaveCombat")